﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 渐变.model;

namespace 渐变
{
    public partial class editCarForm : Form
    {
        /// <summary>
        /// 设置窗体的Region
        /// </summary>
        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 50);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }
        private carInfo _carinfo;

        public editCarForm(model.carInfo carInfo)
        {
            InitializeComponent();
            _carinfo=carInfo;
        }
        public string Value { get; set; }
        public string CTitleValue { get; set; }
        /*给车辆类型下拉菜单绑定数据*/
        public void loadCarType()
        {
            string sql = "select * from CarTypeInfo where CIsDelete=0";
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql);
            cmbCar.DataSource = dt;
            cmbCar.DisplayMember = "CTitle";
            cmbCar.ValueMember = "CTypeId";
        }
        private void editCarForm_Load(object sender, EventArgs e)
        {
            SetWindowRegion();
            loadCarType();
            loadMsg();
        }
        public void loadMsg()
        {

            this.txtCarName.Text = _carinfo.CName;
            this.txtCarNumber.Text = _carinfo.CNumber;
            this.txtRendPrice.Text= _carinfo.CPrice.ToString();
            this.cmbCar.Text = this.CTitleValue;
            this.txtCarImage.Text = _carinfo.CImg;
            if (_carinfo.CStatus)
            {
                this.free.Checked=true;
            }
            else
            {
                this.uiRadioButton2.Checked = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /*修改按钮*/
        private void btnEditCar_Click(object sender, EventArgs e)
        {
            //TestMethod1();
            FileStream fs = new FileStream(txtCarImage.Text.Trim(), FileMode.Open);
            byte[] imageBytes = new byte[fs.Length];
            BinaryReader br = new BinaryReader(fs);
            imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
            string str = Convert.ToBase64String(imageBytes);
            //二进制转换成字符串
            string sql = "update CarInfo set CName=@CName,CNumber=@CNumber,CPrice=@CPrice,CTypeId=@CTypeId,CImg=@CImg,CStatus=@CStatus where CId=@CId and CIsDelete=0";
            SqlParameter[] ps =
        {
               new SqlParameter("@CName",txtCarName.Text),
                new SqlParameter("@CNumber",txtCarNumber.Text),
                new SqlParameter("@CPrice",txtRendPrice.Text),
                new SqlParameter("@CTypeId",cmbCar.SelectedValue),
                new SqlParameter("@CImg",str),
                new SqlParameter("@CId",_carinfo.CId),
                new SqlParameter("@CStatus",free.Checked?"True":"False")
        };

            int fin = DBHelper.ExecuteNonQuery(sql, ps);
            if (fin > 0)
            {
                this.ShowSuccessTip("修改成功");
                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            else
            {
                this.ShowErrorTip("修改失败");
            }
        }

        private void btnSelectCarImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
                txtCarImage.Text = open.FileName;
        }
    }
}
